module.exports = class extends think.cmswing.center {
  // 构造方法
  constructor(ctx) {
    super(ctx);
  }

  async __before() {
    await super.__before();
    if (!this.user.user_pk) {
      const user_code = this.user.user_code;
      const params = {};
      if (this.user.groupid == 9) {
        params.sql = `select pk_customer user_pk from bd_customer where taxpayerid='${user_code}'`;
      } else if (this.user.groupid == 10) {
        params.sql = `select pk_supplier user_pk from bd_supplier where taxpayerid='${user_code}'`;
      }
      const resp = await this.requestNC('executeQuery', params);
      if (resp && resp.errno == 0) {
        this.user.user_pk = resp.data && resp.data.list[0] && resp.data.list[0].user_pk;
      }
    }
  }

  async indexAction() {
    return this.display();
  }

  async accountAction() {
    if (!this.is_login && this.ctx.url.indexOf('/account') > 0) {
      return this.redirect('/weixin/page/index');
    }
    return this.display();
  }

  async account2Action() {
    if (!this.is_login && this.ctx.url.indexOf('/account') > 0) {
      return this.redirect('/weixin/page/index');
    }
    return this.display();
  }

  async contactusAction() {
    return this.display();
  }

  async feigangAction() {
    return this.display();
  }

  async loginAction() {
    const status = this.para('status');
    this.assign('status', status);
    return this.display();
  }

  async newsAction() {
    return this.display();
  }

  async newsshowAction() {
    const id = this.para('id');
    let news = await this.model('document').find(id);
    const article = await this.model('document_article').find(id);
    news.content = article.content;
    this.assign('news', news);
    return this.display();
  }

  async productdetaAction() {
    return this.display();
  }

  async priceAction() {
    return this.display();
  }

  async price_announcementAction() {
    return this.display();
  }

  async checkAction() {
    if (this.config('setup.GEETEST_IS_LOGIN') == 1) {
      // 验证码 钩子
      const signinBefore = await this.hook('loginBefore');
      if (signinBefore === 'no') {
        //const resp = this.fail(-3, '验证码不正确!');
        //this.assign('resp', resp);
        return this.redirect(`/weixin/page/login?status=${res}`);
      }
    }
    // 用户账号密码验证
    const username = this.post('username');
    let password = this.post('password');
    password = encryptPassword(password);
    let res = await this.model('cmswing/member').signin(username, password, this.ip, 5, 0);
    // 钩子
    if (res && res instanceof Object && res.uid > 0) {
      // 记录用户登录行为
      // await this.model("action").log("user_login", "member", res.uid, res.uid, this.ip(), this.http.url);
      // console.log(111111111111121);
      // console.log(res);
      await this.session('webuser', res);
      return this.redirect('/weixin/page/index');
    } else { // 登录失败
      let fail;
      if (res instanceof Array && res.length > 1) {
        await this.session('users', res);
        res = -4;
      }
      switch (res) {
        case -1:
          fail = '用户不存在或被禁用';
          break; // 系统级别禁用
        case -2:
          fail = '密码错误';
          break;
        case -4:
          fail = '该账号对应多个用户';
          break;
        default:
          fail = '未知错误';
          break; // 0-接口参数错误（调试阶段使用）
      }
      //const resp = this.fail(res, fail);
      //this.assign('resp', resp);
      return this.redirect(`/weixin/page/login?status=${res}`);
    }
  }

  // 退出登录
  async logoutAction() {
    await this.session('webuser', null);
    return this.redirect('/weixin/page/index');
  }

};
